//
//  RosterViewController.m
//  RingLetter
//
//  Created by dllo on 16/3/17.
//  Copyright © 2016年 FenglyNuo. All rights reserved.
//

#import "RosterViewController.h"

@interface RosterViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *rostersArr;

@end

@implementation RosterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.rostersArr = [NSMutableArray new];
    
    [self createTableView];
    
    [self createRightAddItem];
}


#pragma mark - 创建 subView
- (void)createRightAddItem {
    UIBarButtonItem *rightAddButton = [[UIBarButtonItem alloc] initWithTitle:@"添加" style:(UIBarButtonItemStylePlain) target:self action:@selector(rightAddButtonAction:)];
    self.navigationItem.rightBarButtonItem = rightAddButton;
    
}

- (void)createTableView {
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:(UITableViewStylePlain)];
    [self.view addSubview:_tableView];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];

}



#pragma mark - 点击方法
- (void)rightAddButtonAction:(UIBarButtonItem *)button {
    
    AddFriendsViewController *addVC = [[AddFriendsViewController alloc] init];
    [self.navigationController pushViewController:addVC animated:YES];
    
}




#pragma mark - tableView Delegate & dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _rostersArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    EMBuddy *buddy = self.rostersArr[indexPath.row];
    cell.textLabel.text = buddy.username;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ChatViewController *CVC = [[ChatViewController alloc] init];
    EMBuddy *buddy = _rostersArr[indexPath.row];
    CVC.chatter = buddy.username;
    CVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:CVC animated:YES];
}




- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.tabBarController.title = @"好友";
    // 从服务器上获取好友数组
    [[EaseMob sharedInstance].chatManager asyncFetchBuddyListWithCompletion:^(NSArray *buddyList, EMError *error) {
        
        if (!error) {
            NSLog(@"获取成功 -- %@", buddyList);
            
            [self.rostersArr removeAllObjects];
            [self.rostersArr addObjectsFromArray:buddyList];
            [self.tableView reloadData];
        }
        
    } onQueue:dispatch_get_main_queue()];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
